package Experimentos;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

import Algoritmos.*;


public class Main {

	private static int N=1000000;
	private static int MAX=1000000;
	private static int nIt=5;
	
	public static int[] getRandomCounting(int max, int n){
		int i;
		Random random=new Random();
		int[] array = new int[n];
		for(i=0;i<n;i++)
			array[i]=random.nextInt(max);
		return array;
	}
	
	public static int[] getRandomRadix(int d, int n){
		int i;
		Random random=new Random();
		int[] array = new int[n];
		int max=(int) Math.pow(10,d);
		for(i=0;i<n;i++){
			int aux=random.nextInt(max);
			while(aux<max/10)
				aux=random.nextInt(max);
			array[i]=aux;
		}
		return array;
	}
	
	public static double[] getRandomBucket(int n){
		int i;
		Random random=new Random();
		double[] array = new double[n];
		for(i=0;i<n;i++)
			array[i]=random.nextDouble();
		return array;
	}
	
	public static void main(String[] args) throws IOException {
		
		long time_start, time_end;
		long[] timeCounting=new long[nIt];
		long[] timeRadix=new long[nIt];
		long[] timeBucket=new long[nIt];
		int i;
		int it;
		int[] A;
		long median;
		double desv;
		
		BufferedWriter outCounting= new BufferedWriter(new FileWriter("CountingSort.txt"));
		for(i=100000;i<=N;i=i+50000){
			for(it=0;it<nIt;it++){
				A = getRandomCounting(MAX,i);		
				//Utils.display(A);
				time_start = System.currentTimeMillis();
				A=CountingSort.countingSort(A, MAX);
				time_end = System.currentTimeMillis();
				timeCounting[it]=time_end-time_start;
				//Utils.display(A);
				//System.out.println("time COUNTING["+it+"]: "+timeCounting[it]);
			}
			//Utils.display(timeCounting);
			median=Utils.getMedian(timeCounting);
			desv=Utils.getDesv(timeCounting,median);
			outCounting.write(median+" "+desv+"\n");						
		}
		outCounting.close();
		String radix="RadixSort";
		int d;
		int[] B;
		for(d=5;d<10;d++){
			BufferedWriter outRadix= new BufferedWriter(new FileWriter(radix+d+".txt"));
			for(i=100000;i<=N;i=i+50000){
				for(it=0;it<nIt;it++){
					B = getRandomRadix(d,i);		
					//Utils.display(B);
					time_start = System.currentTimeMillis();
					RadixSort.radixSort(B, d);
					time_end = System.currentTimeMillis();
					timeRadix[it]=time_end-time_start;
					//Utils.display(B);
					//System.out.println("time RADIX["+it+"]["+d+"]: "+timeRadix[it]);
				}
				median=Utils.getMedian(timeRadix);
				desv=Utils.getDesv(timeRadix,median);
				outRadix.write(median+" "+desv+"\n");
			}
			outRadix.close();
		}
		
		
		BufferedWriter outBucket = new BufferedWriter(new FileWriter("BucketSort.txt"));		
		double[] C;
		for(i=100000;i<=N;i=i+50000){
			for(it=0;it<nIt;it++){
				C = getRandomBucket(i);	
				//Utils.display(C);
				time_start = System.currentTimeMillis();
				BucketSort.bucketSort(C);
				time_end = System.currentTimeMillis();
				timeBucket[it]=time_end-time_start;
				//Utils.display(C);
				//System.out.println("time BUCKET["+it+"]: "+timeBucket[it]);
			}
			median=Utils.getMedian(timeBucket);
			desv=Utils.getDesv(timeBucket,median);
			outBucket.write(median+" "+desv+"\n");		
		}
		outBucket.close();		
	}

}
